<?php

namespace app\index\controller;

use app\common\controller\Frontend;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        $this->redirect('/zQFZdrEgGv.php');
        return $this->view->fetch();
    }

    
//    public function updateAssignKehuinfo(){
//        $page = 0;
//        while(true){
//            $list = db('ec_aiuser_to_ecuser')->field('id,byid,content')->where('status',0)->where('status_code','<>',7777)->limit(1000)->order('id desc')->select();   
//            
//            if(empty($list)) break;
// 
//            foreach ($list as $value){
//                $content = json_decode($value['content'],true);
//                if(!array_key_exists('publicPondId', $content)){
//                    $content['publicPondId']  = '1013213';
//                }
//                if(!array_key_exists('channelId', $content)){
//                    $content['channelId']  = '49995';
//                }
//                $fields =  $content['fields'];
//
//                $duration = db('ec_ai_task_phone')->where('id',$value['byid'])->value('duration');
//                $fields['82733765'] = empty($duration) ? 0 : $duration;
//                $content['fields'] = $fields;
//                db('ec_aiuser_to_ecuser')->where('id',$value['id'])->update([
//                    'status_code'=>7777,
//                    'content'=>json_encode($content,JSON_UNESCAPED_UNICODE)
//                ]);
//
//            }
//        }
//        
//        
//        
//    }


    public  function updateAssignPlan(){
        $today_start_time = strtotime("today");
        $assign_info = db('ec_assign_kehu')->where(['createtime'=>['>',$today_start_time]])->order('id desc')->find();
        if(empty($assign_info)){
            $info = db('ec_assign_kehu')->where(['createtime'=>['<',$today_start_time]])->order('id desc')->find();
            if(!empty($info)){
                $old_assign_id = $info['id'];
                $new_assign_id =db('ec_assign_kehu')->insertGetId([
                    'name'=>'分配计划' .date('Y-m-d'),
                    'count'=>$info['count'],
                    'status'=>0,
                    'auto_assign'=>1,
                    'createtime'=>time()
                ]);
            }
        }else{
            $new_assign_id = $assign_info['id'];
        }
        
        if(empty($old_assign_id)) exit();
        
        $assign_detail_list = db('ec_assign_kehu_detail')->where('assign_id',$old_assign_id)->select();
        if(!empty($assign_detail_list)){
            $end_data =[];
            foreach ($assign_detail_list as $key=>$value){
                $end_data[] = [
                    'assign_id'=>$new_assign_id,
                    'user_id'=>$value['user_id'],
                    'people'=>$value['people'],
                    'createtime'=>time()
                ];
            }
            db('ec_assign_kehu_detail')->insertAll($end_data);
            
        }
        
        
    }
    
    
    
    public function updateTongji(){
        $request_data = $this->request->param();
        $starttime = empty($request_data['date']) ? strtotime('yesterday') : strtotime($request_data['date']);
        $endtime = $starttime + 86400;
        $baiying_assign_people = db('ec_assign_kehu_detail')->where([
            'createtime'=>['between',[$starttime,$endtime]]
        ])->select();
        $list = [];
        foreach ($baiying_assign_people as $value){
            $list[$value['user_id']] =[
                'by_auto_assign'=>$value['success_people'],//自动分配
                'by_shoudong_assign'=>$value['shoudong_fenpei_people'],//手动分配
                'by_assign'=>$value['success_people'] + $value['shoudong_fenpei_people'],//自动分配
                'ecuser_id'=>$value['user_id'],
                'fy_assign'=>0,
                'fy_shoudong_assign'=>0,
                'fy_auto_assign'=>0,
                'createtime'=>$starttime + 3600,
            ];
        }
     
        
        $feiyu_assign_people = db('ec_aiuser_to_ecuser_feiyu')->alias('a')->field('a.ecid,b.type,count(*) count_')
                ->join('ec_feiyu_xiansuo b','a.byid = b.id')
                ->where([
                    'a.createtime'=>['between',[$starttime,$endtime]]
                ])->group('a.ecid,b.type')->select();
      
        foreach ($feiyu_assign_people as $value){
            if(!empty($value['type']) && $value['type'] == 1) {
                $list[$value['ecid']]['fy_shoudong_assign'] = empty($value['count_']) ? 0 : $value['count_'];
            }else{
                $list[$value['ecid']]['fy_auto_assign'] = empty($value['count_']) ? 0 : $value['count_'];
            }            
            $list[$value['ecid']]['ecuser_id'] = empty( $list[$value['ecid']]['ecuser_id']) ? $value['ecid'] :$list[$value['ecid']]['ecuser_id'];
            $list[$value['ecid']]['createtime'] = empty( $list[$value['ecid']]['createtime']) ? $starttime + 3600 :$list[$value['ecid']]['createtime'];
            $list[$value['ecid']]['fy_assign'] = $list[$value['ecid']]['fy_auto_assign'] + (empty($list[$value['ecid']]['fy_shoudong_assign']) ? 0 : $list[$value['ecid']]['fy_shoudong_assign']);            
        }
        
       
//        
//        $str = '{"46":{"by_auto_assign":0,"by_shoudong_assign":0,"by_assign":0,"ecuser_id":46,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"33":{"by_auto_assign":0,"by_shoudong_assign":0,"by_assign":0,"ecuser_id":33,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"7":{"by_auto_assign":30,"by_shoudong_assign":2,"by_assign":32,"ecuser_id":7,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"24":{"by_auto_assign":30,"by_shoudong_assign":0,"by_assign":30,"ecuser_id":24,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"19":{"by_auto_assign":30,"by_shoudong_assign":0,"by_assign":30,"ecuser_id":19,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"42":{"by_auto_assign":0,"by_shoudong_assign":0,"by_assign":0,"ecuser_id":42,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"28":{"by_auto_assign":0,"by_shoudong_assign":0,"by_assign":0,"ecuser_id":28,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"29":{"by_auto_assign":40,"by_shoudong_assign":0,"by_assign":40,"ecuser_id":29,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"34":{"by_auto_assign":30,"by_shoudong_assign":0,"by_assign":30,"ecuser_id":34,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"35":{"by_auto_assign":30,"by_shoudong_assign":0,"by_assign":30,"ecuser_id":35,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"37":{"by_auto_assign":30,"by_shoudong_assign":0,"by_assign":30,"ecuser_id":37,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"38":{"by_auto_assign":30,"by_shoudong_assign":0,"by_assign":30,"ecuser_id":38,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"39":{"by_auto_assign":40,"by_shoudong_assign":2,"by_assign":42,"ecuser_id":39,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"41":{"by_auto_assign":30,"by_shoudong_assign":2,"by_assign":32,"ecuser_id":41,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"25":{"by_auto_assign":0,"by_shoudong_assign":0,"by_assign":0,"ecuser_id":25,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"43":{"by_auto_assign":0,"by_shoudong_assign":0,"by_assign":0,"ecuser_id":43,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"44":{"by_auto_assign":30,"by_shoudong_assign":0,"by_assign":30,"ecuser_id":44,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"47":{"by_auto_assign":0,"by_shoudong_assign":0,"by_assign":0,"ecuser_id":47,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"48":{"by_auto_assign":0,"by_shoudong_assign":0,"by_assign":0,"ecuser_id":48,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"49":{"by_auto_assign":0,"by_shoudong_assign":0,"by_assign":0,"ecuser_id":49,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"50":{"by_auto_assign":0,"by_shoudong_assign":0,"by_assign":0,"ecuser_id":50,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"51":{"by_auto_assign":0,"by_shoudong_assign":0,"by_assign":0,"ecuser_id":51,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"52":{"by_auto_assign":0,"by_shoudong_assign":0,"by_assign":0,"ecuser_id":52,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"14":{"by_auto_assign":30,"by_shoudong_assign":0,"by_assign":30,"ecuser_id":14,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"3":{"by_auto_assign":30,"by_shoudong_assign":2,"by_assign":32,"ecuser_id":3,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"4":{"by_auto_assign":0,"by_shoudong_assign":0,"by_assign":0,"ecuser_id":4,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"5":{"by_auto_assign":30,"by_shoudong_assign":2,"by_assign":32,"ecuser_id":5,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"6":{"by_auto_assign":30,"by_shoudong_assign":0,"by_assign":30,"ecuser_id":6,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"8":{"by_auto_assign":30,"by_shoudong_assign":2,"by_assign":32,"ecuser_id":8,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"9":{"by_auto_assign":30,"by_shoudong_assign":0,"by_assign":30,"ecuser_id":9,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"10":{"by_auto_assign":30,"by_shoudong_assign":0,"by_assign":30,"ecuser_id":10,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"11":{"by_auto_assign":30,"by_shoudong_assign":0,"by_assign":30,"ecuser_id":11,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"12":{"by_auto_assign":0,"by_shoudong_assign":0,"by_assign":0,"ecuser_id":12,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"27":{"by_auto_assign":0,"by_shoudong_assign":0,"by_assign":0,"ecuser_id":27,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"15":{"by_auto_assign":30,"by_shoudong_assign":2,"by_assign":32,"ecuser_id":15,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"16":{"by_auto_assign":30,"by_shoudong_assign":2,"by_assign":32,"ecuser_id":16,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"17":{"by_auto_assign":30,"by_shoudong_assign":1,"by_assign":31,"ecuser_id":17,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"18":{"by_auto_assign":30,"by_shoudong_assign":0,"by_assign":30,"ecuser_id":18,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"20":{"by_auto_assign":0,"by_shoudong_assign":0,"by_assign":0,"ecuser_id":20,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"21":{"by_auto_assign":30,"by_shoudong_assign":0,"by_assign":30,"ecuser_id":21,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"22":{"by_auto_assign":0,"by_shoudong_assign":0,"by_assign":0,"ecuser_id":22,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"2":{"by_auto_assign":30,"by_shoudong_assign":0,"by_assign":30,"ecuser_id":2,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"26":{"by_auto_assign":30,"by_shoudong_assign":0,"by_assign":30,"ecuser_id":26,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"31":{"by_auto_assign":40,"by_shoudong_assign":0,"by_assign":40,"ecuser_id":31,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"45":{"by_auto_assign":40,"by_shoudong_assign":0,"by_assign":40,"ecuser_id":45,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"13":{"by_auto_assign":30,"by_shoudong_assign":2,"by_assign":32,"ecuser_id":13,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"1":{"by_auto_assign":0,"by_shoudong_assign":0,"by_assign":0,"ecuser_id":1,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"40":{"by_auto_assign":0,"by_shoudong_assign":0,"by_assign":0,"ecuser_id":40,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"53":{"by_auto_assign":10,"by_shoudong_assign":0,"by_assign":10,"ecuser_id":53,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"64":{"by_auto_assign":0,"by_shoudong_assign":0,"by_assign":0,"ecuser_id":64,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"60":{"by_auto_assign":0,"by_shoudong_assign":0,"by_assign":0,"ecuser_id":60,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"57":{"by_auto_assign":0,"by_shoudong_assign":0,"by_assign":0,"ecuser_id":57,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"54":{"by_auto_assign":0,"by_shoudong_assign":0,"by_assign":0,"ecuser_id":54,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"65":{"by_auto_assign":0,"by_shoudong_assign":0,"by_assign":0,"ecuser_id":65,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"61":{"by_auto_assign":0,"by_shoudong_assign":0,"by_assign":0,"ecuser_id":61,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"58":{"by_auto_assign":0,"by_shoudong_assign":0,"by_assign":0,"ecuser_id":58,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"55":{"by_auto_assign":0,"by_shoudong_assign":0,"by_assign":0,"ecuser_id":55,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"66":{"by_auto_assign":0,"by_shoudong_assign":0,"by_assign":0,"ecuser_id":66,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"62":{"by_auto_assign":0,"by_shoudong_assign":0,"by_assign":0,"ecuser_id":62,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"59":{"by_auto_assign":0,"by_shoudong_assign":0,"by_assign":0,"ecuser_id":59,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"56":{"by_auto_assign":0,"by_shoudong_assign":0,"by_assign":0,"ecuser_id":56,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"63":{"by_auto_assign":0,"by_shoudong_assign":0,"by_assign":0,"ecuser_id":63,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"68":{"by_auto_assign":30,"by_shoudong_assign":0,"by_assign":30,"ecuser_id":68,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"70":{"by_auto_assign":20,"by_shoudong_assign":0,"by_assign":20,"ecuser_id":70,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"71":{"by_auto_assign":0,"by_shoudong_assign":0,"by_assign":0,"ecuser_id":71,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800},"72":{"by_auto_assign":20,"by_shoudong_assign":0,"by_assign":20,"ecuser_id":72,"fy_assign":0,"fy_shoudong_assign":0,"fy_auto_assign":0,"createtime":1725382800}}';
//        $list = json_decode($str,true);
//        $starttime = 1725379200;
//        $endtime = $starttime + 86400;
      
        
        foreach ($list as $k=>$value){
            $exist_info = db('ec_tongji')->field('id')->where([
                'createtime'=>['between',[$starttime,$endtime]],'ecuser_id'=>$k
            ])->find();   
            
    
            if(empty($exist_info)){
                db('ec_tongji')->insertGetId($value);
            }else{
                unset($value['createtime']);
                db('ec_tongji')->where('id',$exist_info['id'])->update($value);
            }
        }
        
        
        
    }

}
